<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>检测字符的编码</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.mb-decode-numericentity.html">« mb_decode_numericentity</a></li>
      <li style="float: right;"><a href="function.mb-detect-order.html">mb_detect_order »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.mbstring.html">多字节字符串 函数</a></li>
    <li>检测字符的编码</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.mb-detect-encoding" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">mb_detect_encoding</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.6, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">mb_detect_encoding</span> &mdash; <span class="dc-title">检测字符的编码</span></p>

 </div>
   
 <div class="refsect1 description" id="refsect1-function.mb-detect-encoding-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>mb_detect_encoding</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$str</code></span>, <span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$encoding_list</code><span class="initializer"> = mb_detect_order()</span></span>, <span class="methodparam"><span class="type">bool</span> <code class="parameter">$strict</code><span class="initializer"> = false</span></span>): <span class="type">string</span></div>

  <p class="para rdfs-comment">
   检测<span class="type"><span class="type 字符串">字符串</span></span> <code class="parameter">str</code> 的编码。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.mb-detect-encoding-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">str</code></dt>

     <dd>

      <p class="para">
       待检查的<span class="type"><span class="type 字符串">字符串</span></span>。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">encoding_list</code></dt>

     <dd>

      <p class="para">
       <code class="parameter">encoding_list</code> 是一个字符编码列表。
       编码顺序可以由数组或者逗号分隔的列表字符串指定。
      </p>
      <p class="para">
       如果省略了 <code class="parameter">encoding_list</code> 将会使用 detect_order。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">strict</code></dt>

     <dd>

      <p class="para">
       <code class="parameter">strict</code> 指定了是否严格地检测编码。
       默认是 <strong><code>false</code></strong>。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 
 <div class="refsect1 returnvalues" id="refsect1-function.mb-detect-encoding-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   检测到的字符编码，或者无法检测指定字符串的编码时返回 <strong><code>false</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.mb-detect-encoding-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-2565">
    <p><strong>示例 #1 <span class="function"><strong>mb_detect_encoding()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*&nbsp;使用当前的&nbsp;detect_order&nbsp;来检测字符编码&nbsp;*/<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">mb_detect_encoding</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;"auto"&nbsp;将根据&nbsp;mbstring.language&nbsp;来扩展&nbsp;*/<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">mb_detect_encoding</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"auto"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;通过逗号分隔的列表来指定编码列表&nbsp;encoding_list&nbsp;*/<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">mb_detect_encoding</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"JIS,&nbsp;eucjp-win,&nbsp;sjis-win"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">/*&nbsp;使用数组来指定编码列表&nbsp;encoding_list&nbsp;&nbsp;*/<br /></span><span style="color: #0000BB">$ary</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #DD0000">"ASCII"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$ary</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #DD0000">"JIS"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$ary</span><span style="color: #007700">[]&nbsp;=&nbsp;</span><span style="color: #DD0000">"EUC-JP"</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">mb_detect_encoding</span><span style="color: #007700">(</span><span style="color: #0000BB">$str</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$ary</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.mb-detect-encoding-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.mb-detect-order.html" class="function" rel="rdfs-seeAlso">mb_detect_order()</a> - 设置/获取 字符编码的检测顺序</span></li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>